package me.shurufa.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import me.shurufa.R;
import me.shurufa.bean.Book;
import me.shurufa.bean.NoteBook;
import me.shurufa.holder.BaseViewHolder;
import me.shurufa.model.UserBookDigest;
import me.shurufa.model.AddDigestBean;
import me.shurufa.utils.Constants;
import me.shurufa.utils.Utils;

/**
 * 作者 : andy
 * 日期 : 16/3/16 15:55
 * 邮箱 : andyxialm@gmail.com
 * 描述 : 选择书摘本列表适配器
 */
public class SelectDigestBookAdapter extends RecyclerView.Adapter<SelectDigestBookAdapter.SelectDigestBookHolder> {

    private List<NoteBook> dataList;
    private Activity parent;
    private AddDigestBean mAddDigestBean; // 默认选中的书

    public SelectDigestBookAdapter(Activity parent, List<NoteBook> dataList, AddDigestBean addDigestBean) {
        this.dataList = dataList;
        this.parent = parent;
        this.mAddDigestBean = addDigestBean;
    }

    @Override
    public SelectDigestBookHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_digest_book, parent, false);
        SelectDigestBookHolder holder = new SelectDigestBookHolder(v, parent, mAddDigestBean);
        return holder;
    }

    @Override
    public void onBindViewHolder(SelectDigestBookHolder holder, int position) {
        try {
            NoteBook item = dataList.get(position);
            holder.bind(item);
            holder.bindListener(item);
        } catch (Exception e) {
            e.printStackTrace();
            Utils.showToast(R.string.sys_err);
        }
    }

    /**
     * 添加数据在尾部
     * @param items datalist
     */
    public void append(List<NoteBook> items) {
        if (Utils.isNullForList(items)) {
            return;
        }
        int pos = dataList.size();
        dataList.addAll(items);
        notifyItemRangeInserted(pos, items.size());
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public void remove(int position) {
        if (dataList.size() > 0) {
            dataList.remove(position);
            this.notifyItemRemoved(position);
        }
    }

    public void clear() {
        int size = dataList.size();
        dataList.clear();
        this.notifyItemRangeRemoved(0, size);
    }

    class SelectDigestBookHolder extends BaseViewHolder {

        @Bind(R.id.item)
        RelativeLayout item;
        @Bind(R.id.tv_digest_book_name)
        TextView tv_digest_book_name;

        @Bind(R.id.iv_tick)
        ImageView iv_tick;


        public SelectDigestBookHolder(View itemView) {
            super(itemView);
        }

        public SelectDigestBookHolder(View itemView, Object parent) {
            super(itemView, parent);
            ButterKnife.bind(this, itemView);
            addOnClickListener(item);
        }

        public SelectDigestBookHolder(View itemView, Object parent, AddDigestBean addDigestBean) {
            this(itemView, parent);
        }

        public void bind(NoteBook item) {
            tv_digest_book_name.setText(item.title);

            iv_tick.setVisibility(mAddDigestBean.excerptedBookId == item.id ? View.VISIBLE : View.GONE);
        }

        public void bindListener(final NoteBook item) {
            this.item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mAddDigestBean.excerptedBookId = item.id;
                    mAddDigestBean.excerptedBookTitle = item.title;
                    mAddDigestBean.chapterBookId = item.book_id+"";
                    notifyDataSetChanged();
                    sendSelectDigestBookEvent();
                }
            });
        }

        /**
         * 发送选择书摘本Event
         */
        private void sendSelectDigestBookEvent() {
            Intent in = new Intent(Constants.EVENT_SELECT_DIGEST_BOOK);
            in.putExtra(Constants.EVENT_SELECT_DIGEST_BOOK, mAddDigestBean);
            EventBus.getDefault().post(in);
            parent.finish();
        }
    }
}